package com.emm.yixun.mobile.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.model.AddpirModel;

/**
 * <p>
 * Title:添加图片的GridView适配器
 * </p>
 * <p>
 * Description:
 * </p>
 * 
 * @author zhaofei
 * @date 2017年1月5日 下午3:44:42
 */
public class PhoneSelectAdapter extends BaseAdapter {

	Context context;
	ArrayList<AddpirModel> ap;
	int type = 0;// 0详情 1新增或编辑

	public PhoneSelectAdapter(Context context, ArrayList<AddpirModel> ap, int type) {
		this.context = context;
		this.ap = ap;
		this.type = type;
	}

	public void SetDate(ArrayList<AddpirModel> ap) {
		this.ap = ap;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return ap == null ? 0 : ap.size();
	}

	@Override
	public Object getItem(int position) {
		return ap == null ? null : ap.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHoudlers v = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.phone_select_item, null);
			v = new ViewHoudlers();
			v.image_select = (ImageView) convertView.findViewById(R.id.image_select);
			v.image_select2 = (ImageView) convertView.findViewById(R.id.image_select2);
			v.addcontent = (LinearLayout) convertView.findViewById(R.id.addcontent);
			v.delete_image = (ImageView) convertView.findViewById(R.id.delete_image);
			convertView.setTag(v);
		} else {
			v = (ViewHoudlers) convertView.getTag();
		}
		final String photo_path = ap.get(position).getPath();
		EmmApplication.image(R.drawable.xq_img);
		if ("2".equals(ap.get(position).getType())) {
			v.addcontent.setVisibility(View.VISIBLE);
			v.image_select.setVisibility(View.INVISIBLE);
			v.delete_image.setVisibility(View.GONE);
		} else {
			v.addcontent.setVisibility(View.GONE);
			v.image_select.setVisibility(View.VISIBLE);
			if ("1".equals(ap.get(position).getIsnet())) {// 本地
				EmmApplication.imageLoader.displayImage("file://" + photo_path, v.image_select);
			} else if ("2".equals(ap.get(position).getIsnet())) {// 网络
				EmmApplication.imageLoader.displayImage(photo_path, v.image_select);
			}
			if (type == 0) {
				v.delete_image.setVisibility(View.GONE);
			} else {
				v.delete_image.setVisibility(View.VISIBLE);
			}
		}

		v.image_select2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 打开相册选择
				// EmmApplication.IsMoreSelect = true;
				// EmmApplication.Setdate("Picture", "1");
				// context.startActivity(new Intent(context,
				// ImageBucketChooseActivity.class));
				context.sendBroadcast(new Intent("open_Image_popu"));
			}
		});

		// 同步删除 EmmApplication.pir
		v.delete_image.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (ap != null && ap.size() >= position) {
					for (int i = 0; i < EmmApplication.pir.size(); i++) {
						if (EmmApplication.pir.get(i).sourcePath.equals(ap.get(position).getPath())) {
							EmmApplication.pir.remove(i);
							continue;
						}
					}
					ap.remove(position);
					notifyDataSetChanged();
				}
			}
		});

		return convertView;
	}

	public static class ViewHoudlers {
		ImageView image_select;// 图片
		ImageView image_select2;// 添加图片
		ImageView delete_image;// 删除图片
		LinearLayout addcontent;// 添加图片外层布局
	}

}
